package com.company.project.core.ocr;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.company.project.core.common.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * =================================
 * <p>
 * Created by CJNusun on 2021/6/4.
 * <p>
 * 描述: 合合信息OCR识别服务
 */

@Slf4j
public class HeheOCRRecService implements IOCRService {

    public static final String ORC_URL = "http://192.168.101.66:8008/cci_ai/service/v1/text_recog_coordinate";

    public static final String ORC_WORD_URL = "http://192.168.101.66:8008/icr/recognize_document_and_export?word=1";

    public static String recPdfStr(String ocrRecUrl, String path) throws Exception {
        String rec = HttpClientUtils.uploadYunyiDocFile(ocrRecUrl, path, new HashMap<>());
        return rec;
    }

    @Override
    public String recStr(String url, String filePath) throws Exception {
        return rec(url, filePath).getWhole_text();
    }

    public List<LineImageDTO> recPos(String url, String filePath) throws Exception {
        return rec(url, filePath).getLines();
    }

    @Override
    public RotatedImageDTO rec(String url, String filePath) throws Exception {
        String rec = HttpClientUtils.uploadYunyiDocFile(url, filePath, new HashMap<>());
        Map<String, Object> map = JSON.parseObject(rec, Map.class);
        Integer code = (Integer) map.get("code");
        if (code != null && code == 200) {
            RotatedImageDTO result = JSONObject.toJavaObject((JSON) map.get("result"), RotatedImageDTO.class);
            return result;
        } else {
            Object errorMsg = map.get("message");
            if (errorMsg != null) {
                throw new Exception((String) errorMsg);
            }
            throw new Exception("OCR服务接口异常!");
        }
    }

}
